<template>
    <div>
      <!--导航栏-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>

      <el-card>
        <el-row :gutter="20">
          <el-col :span="7">
            <!--输入时可出现清空图标x-->
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
              <template #append>
                <el-button icon="el-icon-search" @click="getGoodsList"></el-button>
              </template>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="goAddGood">添加商品</el-button>
          </el-col>
        </el-row>

        <el-table border stripe :data="goodsList">
          <el-table-column type="index"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column label="商品价格(元)" prop="goods_price" width="110px"></el-table-column>
          <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
          <el-table-column label="创建时间" width="150px">
            <template #default="scope">
              {{scope.row.add_time|dateFormat}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #default="scope">
              <el-button size="mini" class="el-icon-edit" type="primary" @click="showEditGoodDialog(scope.row)"></el-button>
              <el-button size="mini" class="el-icon-delete" type="danger" @click="deleteGood(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--分页器-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10, 25, 50, 100]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>

      <!--编辑商品对话框-->
      <el-dialog
        title="编辑商品"
        :visible.sync="editGoodDialogVisible"
        width="50%"
        @close="editGoodDialogClosed"
      >
        <el-form label-width="70px" :model="editGoodForm" :rules="editGoodFormRules" ref="editGoodFormRef">
          <el-form-item label="商品名" prop="goods_name">
            <el-input v-model="editGoodForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="editGoodForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="editGoodForm.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
        <span class="dialog-footer">
    <el-button @click="editGoodDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editGood">确 定</el-button>
  </span>
        </template>
      </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'Goods',
  data () {
    return {
      queryInfo: {
        query: '', // 关键字
        pagenum: 1, // 页码
        pagesize: 20 // 条数
      },
      goodsList: [],
      total: 0, // 用户总数
      editGoodDialogVisible: false,
      editGoodForm: [],
      editGoodFormRules: {
        goods_name: [
          {
            required: true,
            message: '请输入商品名',
            trigger: 'blur'
          }
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur'
          }
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (data.meta.status !== 200) return this.$message.error('获取商品列表失败')
      // 获取成功
      this.goodsList = data.data.goods
      this.total = data.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (current) {
      this.queryInfo.pagenum = current
      this.getGoodsList()
    },
    formatDate (date) {
      const date1 = new Date(date)
      return `${date1.getFullYear()}-${date1.getMonth() + 1}-${date1.getDate()} ${date1.getHours()}:${date1.getMinutes()}:${date1.getSeconds()}`
    },
    async deleteGood (row) {
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return this.$message.info('取消删除')
      const { data: res } = this.$http.delete('goods/' + row.goods_id)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    async showEditGoodDialog (row) {
      this.editGoodDialogVisible = true
      const { data: res } = await this.$http.get('goods/' + row.goods_id)
      if (res.meta.status !== 200) return this.$message.error('获取商品信息失败')
      this.$message.success('获取商品信息成功')
      this.editGoodForm = res.data
    },
    editGoodDialogClosed () {
      this.editGoodForm = []
    },
    editGood () {
      this.$refs.editGoodFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写完整的商品信息')
        const { data: res } = await this.$http.put(`goods/${this.editGoodForm.goods_id}`, this.editGoodForm)
        if (res.meta.status !== 200) return this.$message.error('编辑商品失败')
        this.$message.success('编辑商品成功')
        this.editGoodDialogVisible = false
        this.getGoodsList()
      })
    },
    goAddGood () {
      this.$router.push('/goods/AddGood')
    }
  }
}
</script>

<style scoped>

</style>
